package com.waypolice.manager.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.waypolice.base.BaseController;
import com.waypolice.pojo.*;
import com.waypolice.manager.service.UpmsDictionaryService;
import com.waypolice.manager.service.UpmsFileNumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 档号格式controller
 * @author  lzh on 2018/3/29.
 */

@Controller
@Api(value = "档号格式", description = "档号格式")
@RequestMapping("/manage/filenum")
public class UpmsFileNumController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsFileNumController.class);

    @Autowired
    private UpmsFileNumService upmsFileNumService;

    @Autowired
    private UpmsDictionaryService upmsDictionaryService;

    @ApiOperation(value = "档号格式首页")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/filenum/index";
    }

    @ApiOperation(value = "查询字典著录项分类")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/read", method = RequestMethod.POST)
    @ResponseBody
    public JSONArray selectDictionary(){
        JSONArray jsonList = new JSONArray();
        JSONObject jsonObject0 = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        UpmsDictionaryExample dictionaryExample = new UpmsDictionaryExample();
        jsonObject0.put("id",0);
        jsonObject0.put("pId",0);
        jsonObject0.put("name","档号格式");
        jsonObject0.put("open",true);
        jsonList.add(jsonObject0);
        //传统档号
        dictionaryExample.or().andCodeEqualTo("tradition");
        List<UpmsDictionary> upmsDictionaries = upmsDictionaryService.selectByExample(dictionaryExample);
        jsonObject1.put("id",upmsDictionaries.get(0).getCode());
        jsonObject1.put("pId",0);
        jsonObject1.put("name",upmsDictionaries.get(0).getDescription());
        jsonList.add(jsonObject1);
        //单件档号
        dictionaryExample.clear();
        dictionaryExample.or().andCodeEqualTo("singleArchive");
        List<UpmsDictionary> upmsDictionaries1 = upmsDictionaryService.selectByExample(dictionaryExample);

        jsonObject2.put("id",upmsDictionaries1.get(0).getCode());
        jsonObject2.put("pId",0);
        jsonObject2.put("name",upmsDictionaries1.get(0).getDescription());
        jsonList.add(jsonObject2);
        return jsonList;
    }
    @ApiOperation(value = "分隔符列表")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/selectSeparator",method = RequestMethod.GET)
    @ResponseBody
    public List<UpmsDictionary> selectSeparator(){
        UpmsDictionaryExample example = new UpmsDictionaryExample();
        example.or().andFatherCodeEqualTo("separator");
        List<UpmsDictionary> upmsDictionaries = upmsDictionaryService.selectByExample(example);
        return upmsDictionaries;
    }
    @ApiOperation(value = "对应的所有著录项列表")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/selectCode",method = RequestMethod.GET)
    @ResponseBody
    public List<UpmsDictionary> selectCode(String pkCode){
        UpmsDictionaryExample example = new UpmsDictionaryExample();
        example.or().andFatherCodeEqualTo(pkCode);
        List<UpmsDictionary> upmsDictionaries = upmsDictionaryService.selectByExample(example);
        return upmsDictionaries;
    }


    @ApiOperation(value = "查出对应的档号格式")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    @ResponseBody
    public List<UpmsFileNum> index(String fondsId,String pkCode){
        UpmsFileNumExample fileNumExample = new UpmsFileNumExample();
        fileNumExample.setOrderByClause("SORT_NUM asc");
        fileNumExample.or().andStr1EqualTo(fondsId).andPkCodeEqualTo(pkCode);
        List<UpmsFileNum> upmsFileNums = upmsFileNumService.selectByExample(fileNumExample);
        return upmsFileNums;

    }
    @ApiOperation(value = "查出指定著录项信息")
    @RequiresPermissions("upms:filenum:read")
    @RequestMapping(value = "/code",method = RequestMethod.POST)
    @ResponseBody
    public Integer selectOneCode(String nowCode){
        Integer len = 0;
        UpmsFileNumExample fileNumExample = new UpmsFileNumExample();
        fileNumExample.or().andCodeEqualTo(nowCode);
        List<UpmsFileNum> upmsFileNums = new ArrayList<>();
        upmsFileNums = upmsFileNumService.selectByExample(fileNumExample);
        if (!upmsFileNums.isEmpty()){
            String str1 = upmsFileNums.get(0).getLength();
            if (str1==null||str1.length()==0) {
                len = 999;
            }else {
                len = Integer.parseInt(str1);
            }
        }else{
            UpmsDictionaryExample example = new UpmsDictionaryExample();
            example.or().andCodeEqualTo(nowCode);
            List<UpmsDictionary> upmsDictionaries = upmsDictionaryService.selectByExample(example);
            String str1 = upmsDictionaries.get(0).getStr1();
            if (str1==null||str1.length()==0) {
                len = 999;
            }else {
                len = Integer.parseInt(str1);
            }
        }
        if (len == null || len ==0){
            len = 999;
        }
        return len;
    }

    @ApiOperation(value = "添加档号")
    @RequiresPermissions("upms:filenum:create")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public Integer insert(UpmsFileNum upmsFileNum){
        int count = 0;
        if (upmsFileNum.getFileId() == null ){
            //执行添加方法
            UpmsFileNumExample example = new UpmsFileNumExample();
            example.createCriteria().andCodeEqualTo(upmsFileNum.getCode()).andPkCodeEqualTo(upmsFileNum.getPkCode()).andStr1EqualTo(upmsFileNum.getStr1());
            List<UpmsFileNum> upmsFileNums = upmsFileNumService.selectByExample(example);
            if (upmsFileNums.isEmpty()) {
                count = upmsFileNumService.insert(upmsFileNum);
            }else{
                count = 4;
            }

        }
        return count;

    }
    @ApiOperation(value = "编辑档号")
    @RequiresPermissions("upms:filenum:update")
    @RequestMapping(value = "/update1",method = RequestMethod.POST)
    @ResponseBody
    public Integer editFileNum(UpmsFileNum upmsFileNum){
        int count = 0;
        if (upmsFileNum.getFileId() != null ){
            List<String> codes = getCodes();
            //执行编辑方法
            //判断是否是内置对象
            UpmsFileNum fileNum = upmsFileNumService.selectByPrimaryKey(upmsFileNum.getFileId());
            boolean isBuilt = false;
            for (String code:codes){
                //如果是内置对象，给个标识
                if (fileNum.getCode().equals(code)){
                    isBuilt = true;
                    //判断内置对象是否修改的是名称，如果是返回2,如果名称未修改，循环结束
                    if (!upmsFileNum.getCode().equals(fileNum.getCode())){
                        count = 2;
                        return count;
                    }else {
                        break;
                    }
                }
            }
            //如果是内置对象且未修改名称
            if (isBuilt){
                count = upmsFileNumService.updateByPrimaryKey(upmsFileNum) + 2;
            }else{
                //不是内置对象，查出修改的名称是否已经存在
                UpmsFileNumExample example = new UpmsFileNumExample();
                example.createCriteria().andCodeEqualTo(upmsFileNum.getCode()).andPkCodeEqualTo(upmsFileNum.getPkCode()).andStr1EqualTo(upmsFileNum.getStr1());
                List<UpmsFileNum> upmsFileNums = upmsFileNumService.selectByExample(example);
                //如果查出不存在，直接修改
                if (upmsFileNums.isEmpty()){
                    count = upmsFileNumService.updateByPrimaryKey(upmsFileNum) + 2;
                    //如果档号组成项存在
                }else {
                    //判断除当前编辑对象外是否还有此名称存在
                    if (upmsFileNums.get(0).getFileId().equals(upmsFileNum.getFileId()) ){
                        count = upmsFileNumService.updateByPrimaryKey(upmsFileNum) + 2;
                    }else{
                        count = 4;
                    }
                }
            }
        }
        return count;
    }

    @ApiOperation(value = "删除档号某一项")
    @RequiresPermissions("upms:filenum:delete")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Integer delete(Integer fileId){
        List<String> codes = getCodes();
        Integer count = 0;
        Boolean isExist = false;
        if (fileId !=null && fileId > 0){
            UpmsFileNum fileNum = upmsFileNumService.selectByPrimaryKey(fileId);
            for (String code : codes){
                if (code.equals(fileNum.getCode())){
                    isExist = true;
                    break;
                }
            }
            if (isExist){
                count = 2;
                return count;
            }
            count = upmsFileNumService.deleteByPrimaryKey(fileId);
        }
        return count;
    }

    @ApiOperation(value = "上下移档号")
    @RequiresPermissions("upms:filenum:update")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Integer update(Integer id1,Integer num1,Integer id2,Integer num2){
        UpmsFileNum upmsFileNum = upmsFileNumService.selectByPrimaryKey(id1);
        upmsFileNum.setSortNum(num2);
        int i = upmsFileNumService.updateByPrimaryKey(upmsFileNum);
        UpmsFileNum upmsFileNum1 = upmsFileNumService.selectByPrimaryKey(id2);
        upmsFileNum1.setSortNum(num1);
        i = upmsFileNumService.updateByPrimaryKey(upmsFileNum1) + 1;
        return i;

    }

    public List<String> getCodes(){
        List<String> codes = new ArrayList<>();
        codes.add("volFian");
        codes.add("volStorageTime");
        codes.add("volNo");
        codes.add("volClass");
        codes.add("volArchiveYear");
        codes.add("fixedValue");
        codes.add("itemFian");
        codes.add("itemExp1");
        codes.add("itemNum");
        codes.add("itemExp3");
        return codes;
    }
}